/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.rbh.risk.managedbean.frontend;

import com.rbh.risk.entity.Workgroup;
import com.rbh.risk.model.SummaryLevelModel;
import com.rbh.risk.service.RiskDataService;
import com.rbh.risk.service.SeverityLevelService;
import com.rbh.risk.service.WorkgroupService;
import com.rbh.risk.util.Constants;
import com.rbh.risk.util.DateUtil;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import org.primefaces.event.SelectEvent;

/**
 * @author M
 */
@ManagedBean(name = "riskSummaryBean")
@SessionScoped
public class RiskSummaryBean {

    private static final long serialVersionUID = 1L;
    /*
     * Backing Beans
     */
    @ManagedProperty(value = "#{riskListBean}")
    private RiskListBean riskListBean;
    @ManagedProperty(value = "#{privilegeBean}")
    private PrivilegeBean privilegeBean;
    /*
     * Selected Object
     */
    @ManagedProperty(value = "#{selectedDateTimeFrom}")
    private Date selectedDateTimeFrom;
    @ManagedProperty(value = "#{selectedDateTimeTo}")
    private Date selectedDateTimeTo;
    @ManagedProperty(value = "#{fromMinDate}")
    private Date fromMinDate;
    @ManagedProperty(value = "#{toMinDate}")
    private Date toMinDate;
    @ManagedProperty(value = "#{maxDate}")
    private Date maxDate;
    private boolean minorMode;
    private Integer workgroupId;
    /*
     * Object
     */
    @ManagedProperty(value = "#{summaryLevels}")
    private List<SummaryLevelModel> summaryLevels;
    /*
     * Service
     */
    @ManagedProperty(value = "#{riskDataService}")
    private RiskDataService riskDataService;
    @ManagedProperty(value = "#{workgroupService}")
    private WorkgroupService workgroupService;
    @ManagedProperty(value = "#{severityLevelService}")
    private SeverityLevelService severityLevelService;

    public RiskSummaryBean() {
    }

    @PostConstruct
    private void init() {
        minorMode = false;
        workgroupId = null;
        selectedDateTimeFrom = DateUtil.adjustDate(new Date(), 0, -1, 0);
        selectedDateTimeTo = DateUtil.adjustDate(new Date(), 0, 0, 0);
        this.initfindMajor();
        riskListBean.init();
    }

    private void initfindMajor() {
        summaryLevels = riskDataService.getMajorSummaryLevels(privilegeBean.getHashMapACL(), selectedDateTimeFrom, selectedDateTimeTo);
    }

    private void initfindMinor(Integer workgroupId, boolean skip) throws IOException {
        Workgroup data = workgroupService.getById(workgroupId);
        if (skip || data.getWorkgroupLevel().getWorkgroupLevelId() != 1) {
            this.workgroupId = workgroupId;
            summaryLevels = riskDataService.getMinorSummaryLevels(data, selectedDateTimeFrom, selectedDateTimeTo);
        } else {
            this.injectLevelId(workgroupId, null);
        }
    }

    public void handleDateSelectFrom(SelectEvent event) throws IOException{
        selectedDateTimeFrom = (Date) event.getObject();
        if (minorMode) {
            this.initfindMinor(workgroupId, true);
        } else {
            this.initfindMajor();
        }
    }

    public void handleDateSelectTo(SelectEvent event) throws IOException {
        selectedDateTimeTo = (Date) event.getObject();
        if (minorMode) {
            this.initfindMinor(workgroupId, true);
        } else {
            this.initfindMajor();
        }
    }

    public void handleCmdLink(Integer workgroupId) throws IOException {
        minorMode = true;
        this.initfindMinor(workgroupId, false);
    }

    public void injectLevelId(Integer workgroupId, Integer levelId) throws IOException {
        riskListBean.init();
        riskListBean.setWorkgroup(workgroupService.getById(workgroupId));
        if (levelId != null) {
            riskListBean.setLevel(severityLevelService.getById(levelId));
        }
        riskListBean.setSelectedDateTimeFrom(selectedDateTimeFrom);
        riskListBean.setSelectedDateTimeTo(selectedDateTimeTo);
        riskListBean.initfindRisk();

        FacesContext.getCurrentInstance().getExternalContext().redirect("../pages/riskList.xhtml");
    }

    public String reset() {
        this.init();
        return Constants.PATH_SUMMARY;
    }

    /*
     * Getter & Setter
     */
    public Date getSelectedDateTimeFrom() {
        return selectedDateTimeFrom;
    }

    public void setSelectedDateTimeFrom(Date selectedDateTimeFrom) {
        this.selectedDateTimeFrom = selectedDateTimeFrom;
    }

    public Date getSelectedDateTimeTo() {
        return selectedDateTimeTo;
    }

    public void setSelectedDateTimeTo(Date selectedDateTimeTo) {
        this.selectedDateTimeTo = selectedDateTimeTo;
    }

    public Date getFromMinDate() {
        return DateUtil.adjustDate(selectedDateTimeFrom, 0, -11, 0);
    }

    public void setFromMinDate(Date fromMinDate) {
        this.fromMinDate = fromMinDate;
    }

    public Date getToMinDate() {
        return DateUtil.adjustDate(selectedDateTimeFrom, 0, 0, 0);
    }

    public void setToMinDate(Date toMinDate) {
        this.toMinDate = toMinDate;
    }

    public Date getMaxDate() {
        return DateUtil.resetTime(new Date());
    }

    public void setMaxDate(Date maxDate) {
        this.maxDate = maxDate;
    }

    public List<SummaryLevelModel> getSummaryLevels() {
        return summaryLevels;
    }

    public void setSummaryLevels(List<SummaryLevelModel> summaryLevels) {
        this.summaryLevels = summaryLevels;
    }

    public RiskDataService getRiskDataService() {
        return riskDataService;
    }

    public void setRiskDataService(RiskDataService riskDataService) {
        this.riskDataService = riskDataService;
    }

    public WorkgroupService getWorkgroupService() {
        return workgroupService;
    }

    public void setWorkgroupService(WorkgroupService workgroupService) {
        this.workgroupService = workgroupService;
    }

    public RiskListBean getRiskListBean() {
        return riskListBean;
    }

    public void setRiskListBean(RiskListBean riskListBean) {
        this.riskListBean = riskListBean;
    }

    public SeverityLevelService getSeverityLevelService() {
        return severityLevelService;
    }

    public void setSeverityLevelService(SeverityLevelService severityLevelService) {
        this.severityLevelService = severityLevelService;
    }

    public boolean isMinorMode() {
        return minorMode;
    }

    public void setMinorMode(boolean minorMode) {
        this.minorMode = minorMode;
    }

    public Integer getWorkgroupId() {
        return workgroupId;
    }

    public void setWorkgroupId(Integer workgroupId) {
        this.workgroupId = workgroupId;
    }

    public PrivilegeBean getPrivilegeBean() {
        return privilegeBean;
    }

    public void setPrivilegeBean(PrivilegeBean privilegeBean) {
        this.privilegeBean = privilegeBean;
    }
}